package org.elec.mac.utils;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.elec.mac.constants.Constants;
import org.elec.mac.entity.SUnifiedOrder;
import org.elec.mac.exception.ErrorCode;
import org.elec.mac.service.SUnifiedOrderService;
import org.elec.mac.wxpay.WXPayConfigImpl;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

@Slf4j

/**
 * Created by bingren on 2018/5/17.
 */
public class UnifiedOrder {

    public static JSONObject order(String oid, String machserial, String openid, String tradetype, JSONObject result, String out_trade_no, String spbill_create_ip, Long acprice, String appid, String mchid, String key, String certpath, String notify_url,SUnifiedOrderService sUnifiedOrderService){
        Map<String, String> body = new HashMap<>();
        try {
            String[] array = spbill_create_ip.split(",");
            WXPayConfigImpl config = WXPayConfigImpl.getInstance(appid, mchid, key, certpath);
            WXPay wxPay = new WXPay(config);

            HashMap<String, String> data = new HashMap<>();
            data.put("body", "早餐机订单：" + out_trade_no);
            data.put("out_trade_no", out_trade_no);
            data.put("fee_type", "CNY");
            data.put("total_fee", String.valueOf(acprice));
            data.put("spbill_create_ip", StringUtils.trim(array[0]));
            data.put("notify_url", notify_url);
            data.put("trade_type", tradetype);
            data.put("product_id", oid);
            data.put("attach", oid);
            data.put("openid", openid);
            if("NATIVE".equals(tradetype)) {
                log.info("device_info:"+machserial);
                data.put("device_info", machserial);
            }

            try {
                Map<String, String> r = wxPay.unifiedOrder(data);
                log.info("----------------------------------------------------------------------");
                log.info("统一下单响应数据："+r);
                body.put("package", "prepay_id=" + r.get("prepay_id"));
                body.put("timeStamp", String.valueOf(System.currentTimeMillis()));
                body.put("nonceStr", WXPayUtil.generateNonceStr());
                body.put("signType", "MD5");
                body.put("appId", appid);
                body.put("paySign", WXPayUtil.generateSignature(body, key, WXPayConstants.SignType.MD5));
                result.put("body", body);

                SUnifiedOrder sUnifiedOrder = new SUnifiedOrder();
                if("NATIVE".equals(tradetype)) {
                    sUnifiedOrder.setDeviceInfo(data.get("device_info"));
                }
                sUnifiedOrder.setOpenid(data.get("openid"));
                sUnifiedOrder.setAppid(appid);
                sUnifiedOrder.setBody(data.get("body"));
                sUnifiedOrder.setOutTradeNo(data.get("out_trade_no"));
                sUnifiedOrder.setMchId(mchid);
                sUnifiedOrder.setFeeType(data.get("fee_type"));
                sUnifiedOrder.setTotalFee(Utils.parseInt(data.get("total_fee"), Constants.UnknowInteger));
                sUnifiedOrder.setSpbillCreateIp(data.get("spbill_create_ip"));
                sUnifiedOrder.setNotifyUrl(data.get("notify_url"));
                sUnifiedOrder.setTradeType(data.get("trade_type"));
                sUnifiedOrder.setAttach(data.get("attach"));
                sUnifiedOrder.setProductId(data.get("product_id"));
                sUnifiedOrderService.insert(sUnifiedOrder);

            } catch(Exception ex) {
                result.put("code", ErrorCode.ERR_FAILED);
                result.put("msg", "[" + ErrorCode.ERR_FAILED + "]统一下单错误");
                ex.printStackTrace();
                return result;
            }
        } catch(Exception ex) {
            result.put("code", ErrorCode.ERR_FAILED);
            result.put("msg", "[" + ErrorCode.ERR_FAILED + "]获取订单信息错误");
            ex.printStackTrace();
            return result;
        }
        return result;
    }
}
